<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Model\Admin\CallList;

class AdminController extends Controller
{
    /**
     * 管理员首页
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $operator = $request->input('operator');

        switch ($operator) {

            case 'list':
                
                $status = $request->input('status');

                $list = CallList::where('status', $status)->orderBy('count', 'desc')->orderBy('create_time', 'desc')->get();
                $count = CallList::count();

                $data = array(
                    'total' => ceil($count/10),
                    'page' => 1,
                    'records' => $count,
                    'rows' => $list,
                    'status' => $status
                );

                return response()->json($data);

                break;
            
            case 'list_compare':
                
                $status = 0;

                if ($list = CallList::select('id', 'count', 'mobile', 'position', 'address', 'remark')->where('status', $status)->get()) {
                    
                    $data = array('status' => 'success','list' => $list);

                }else{

                    $data = array('status' => 'error');

                }

                return response()->json($data);

                break;

            case 'status_edit':
                
                $id = $request->input('id');
                $status = $request->input('status');

                if (CallList::where('id', $id)->update(['status' => $status])) {
                    
                    $data = array('status' => 'success');

                }else{

                    $data = array('status' => 'error');
                }

                return response()->json($data);

                break;

            case 'address_get':
                
                $id = $request->input('id');
                $positionList = explode(';', $request->input('position'));
                $address = '';

                foreach ($positionList as $key => $position) {
                    
                    $address_data = json_decode(file_get_contents('http://apis.map.qq.com/ws/geocoder/v1/?location=' . 
                        explode(',', $position)[0] . ',' . explode(',', $position)[1] . 
                        '&key=2OQBZ-7HERF-I44JO-JZDD5-JGPX7-VRB7R&get_poi=1'));

                    if ($key == 0) {
                        
                        $address .= ($address_data->status == 0) ? $address_data->result->address : '';

                    }else{

                        $address .= ($address_data->status == 0) ? ';' . $address_data->result->address : ';';
                    }
                }

                if (CallList::where('id', $id)->update(['address' => $address])) {
                    
                    $data = array('status' => 'success', 'address' => $address);

                }else{

                    $data = array('status' => 'error');
                }

                return response()->json($data);

                break;

            default:
                break;
        }

        return view('Admin.admin.index');
    }

    
}
